package Assignment.Test3;

import java.util.ArrayList;
import java.util.Scanner;

public class BookSystem {
    public static void main(String[] args) {

        ArrayList<Book> list = new ArrayList<>();

        Book book = new Book("001", "活着", "余华", "66.6");
        list.add(book);

        System.out.println("----------欢-迎-进-入-图-书-管-理-系-统----------");
        while (true) {
            Scanner scanner = new Scanner(System.in);
            System.out.println();
            System.out.print("请输入选项（1.添加图书 2。删除图书 3.修改图书 4查询图书 5.展示全部图书 6.退出）:");
            String choose = scanner.next();
            switch (choose) {
                case "1" -> Augment(list);
                case "2" -> Delete(list);
                case "3" -> Amend(list);
                case "4" -> Find(list);
                case "5" -> Show(list);
                case "6" -> {
                    System.out.println("退出成功！");
                    System.exit(0);
                }
                default -> System.out.println("没有此选项");
            }

        }
    }

    //增
    public static void Augment(ArrayList<Book> list) {
        Scanner sc = new Scanner(System.in);
        Book book = new Book();
        while (true){
            System.out.print("请输入图书编号：");
            String id = sc.next();
            if (!solo(list, id)) {
                book.setId(id);
                break;
            }else {
                System.out.println("图书编号已存在，请重新输入");
            }
        }
        System.out.print("请输入书名：");
        book.setBookName(sc.next());
        System.out.print("请输入作者：");
        book.setWriter(sc.next());
       while (true){
           System.out.print("请输入价格：");
           String price = sc.next();
           if (Number(price)) {
               book.setPrice(price);
               break;
           }else {
               System.out.println("只能输入数字");
           }
       }
       list.add(book);
        System.out.println("添加成功！");
    }

    //删
    public static void Delete(ArrayList<Book> list) {
        if (list == null){
            System.out.println("当前无图书信息，请添加后再对其进行操作");
            return;
        }
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.print("请输入要删除的图书编号：");
            String id = sc.next();
            if (solo(list,id)){
                Book book = list.remove(indexBook(list,id));
                System.out.println("删除成功！");
                System.out.println(book.getId() + "\t\t" + book.getBookName() + "\t\t" +
                        book.getWriter() + "\t\t" + book.getPrice());
                break;
            }else {
                System.out.println("图书编号不存在，请重新输入");
            }
        }

    }

    //改
    public static void Amend(ArrayList<Book> list) {
        if (list == null){
            System.out.println("当前无图书信息，请添加后再对其进行操作");
            return;
        }
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.print("请输入要修改的图书编号：");
            String id = sc.next();
            if (solo(list,id)){

                int index = indexBook(list,id);
                System.out.print("请输入书名：");
                list.get(index).setBookName(sc.next());
                System.out.print("请输入作者：");
                list.get(index).setWriter(sc.next());
                while (true){
                    System.out.print("请输入价格：");
                    String price = sc.next();
                    if (Number(price)) {
                        list.get(index).setPrice(price);
                        break;
                    }else {
                        System.out.println("只能输入数字");
                    }
                }
                System.out.println("修改成功！");
                break;
            }else{
                System.out.println("当前图书编号不存在，请重新输入");
            }
        }
    }

    //查
    public static void Find(ArrayList<Book> list) {
        if (list == null){
            System.out.println("当前无图书信息，请添加后再对其进行操作");
            return;
        }
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("请输入要查询的图书编号：");
            String id = sc.next();
            if (solo(list, id)) {
                int index = indexBook(list,id);
                System.out.println(list.get(index).getId() + "\t\t" + list.get(index).getBookName() + "\t\t" +
                        list.get(index).getWriter() + "\t\t" + list.get(index).getPrice());
                System.out.println("查询成功！");
                break;

            } else {
                System.out.println("当前图书编号不存在，请重新输入");
            }
        }
    }

    //示
    public static void Show(ArrayList<Book> list) {
        System.out.println();
        System.out.println("图书编号\t\t书名\t\t作者\t\t价格");
        for (int i = 0; i < list.size(); i++) {
            Book book = list.get(i);
            System.out.println(book.getId() + "\t\t" + book.getBookName() + "\t\t" +
                    book.getWriter() + "\t\t" + book.getPrice());

        }
    }


    //判断图书编号是否唯一
    public static boolean solo(ArrayList<Book> list, String id) {
        return indexBook(list, id) >= 0;
    }

    //通过图书编号查找索引
    public static int indexBook(ArrayList<Book> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }

    //判断是否是数字
    public static boolean Number(String str){
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (!(c>='0'&&c<='9')){
                return false;
            }
        }
        return true;
    }
}
